ResetTextLocation:
	push hl
	ld hl, 0
	ld (TextX), hl
	pop hl
	ret

; Draws the character A at TextX, TextY
PutChar:
	push hl
	push bc
	push de
	push af
	ld de, 5
	sub 20h
	call DEMulA
	ex de,hl
	ld ix, Font
	add ix, de
	ld b, 5
	ld a, (TextX)
	ld d, a
	add a, 4
	ld (TextX), a
	ld a, (TextY)
	ld e, a
	call PutSpriteOR
	pop af
	pop de
	pop bc
	pop hl
	ret

; Measures (HL) and returns width, height in b, c
MeasureString:
	ld bc, 0
	ld a, (hl)
	inc hl
	cp '\n'
	jr z, MeasureStringNewline
	cp '\r'
	jr z, MeasureStringNewline
	or a
	ret z
	ld a, 4
	add a,b
	jr MeasureString
	ret
MeasureStringNewline:
	ld a, 6
	add a,c
	ret

; Draws (HL) at TextX, TextY
DrawString:
	call SafeInterruptDI
	push af
_	ld a, (hl)
	inc hl
	cp '\n'
		jr z, DrawStringNewline
	cp '\r'
		jr z, DrawStringNewline
	or a
		jr z,_
	call PutChar
	jr -_
DrawStringNewline:
	ld a, (TextY)
	add a, 6
	ld (TextY), a
	ld a, 0
	ld (TextX), a
	jr -_
_	pop af
	ret c
	ei
	ret
;Smartly adds a newline (shifts screen if at bottom)
Newline:
	xor a
	ld (TextX),a
	ld a,(TextY)
	add a, 6
	cp 55
		call nc,Shift
	ld (TextY),a
	ret
;Shifts the whole screen up six pixels and updates each time
Shift:
#ifdef DEBUG
#ifdef SCROLL_WAIT
	push hl
		push de
			call FastCopy
			call FlushKeys
			call WaitKey
		pop de
	pop hl
#endif
#endif
	ld a,1
	ld b,6
	ld c,0	;;
ShiftL:
		call LCDUp
		djnz ShiftL
	ld a,54
	ret
; Draws (HL) at TextX, TextY
DrawWrappedString:
	call SafeInterruptDI
	push af
_	ld a, (hl)
	inc hl
	cp '\n'
		jr z, WrapLine
	cp '\r'
		jr z, WrapLine
	or a
		jr z,_
	call PutChar
	ld a,(TextX)
	cp 93
		jr nc,WrapLine
	jr -_
WrapLine:
	call Newline
	jr -_
_	pop af
	ret c
	ei
	ret
DispHexA:
   push ix
   push af
   push hl
   push bc
   push af
   rrca
   rrca
   rrca
   rrca
   call dispha
   pop af
   call dispha
   pop bc
   pop hl
	pop af
	pop ix
   ret
dispha:
   and 15
   cp 10
   jp nc,dhlet
   add a,48
   jp dispdh
dhlet:
   add a,55
dispdh:
   call PutChar
   ret
;
DispDecHL:
	call SafeInterruptDI
	push af
	ld de,TextBuffer
	push de
	call Num2Dec
	pop hl
	push hl
	call DrawString
	pop ix
	call FreeMem
	pop af
		jr c,$+3
	ei
	return(SUCCESS)
_	pop hl
	pop af
		ret c
	ei
	ret
DispDecA:
	ld l,a
	ld h,0
	call SafeInterruptDI
	push af
	ld de,TextBuffer
	push de
	call Num2Dec
	pop hl
	push hl
	inc hl
	inc hl
	ld a,(hl)
	cp '0'
		jr nz,$+3
	inc hl
	ld a,(hl)
	cp '0'
		jr nz,$+3
	inc hl
	call DrawString
	pop ix
	call FreeMem
	pop af
		jr c,$+3
	ei
	return(SUCCESS)
_	pop hl
	pop af
		ret c
	ei
	ret
